package edu.utility.Gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import sun.misc.*;
import com.google.gson.Gson;
import com.sun.org.apache.xml.internal.security.utils.Base64;

import edu.utility.Entities.Entities;
import edu.utility.Entities.Network;
import edu.utility.Entities.User;

public class LogInPanel extends JPanel{
	private JButton logIn, exit;
	private JTextField emailField;
	private JPasswordField passwordField;
	private MyFrame frame;
	private JLabel logInErrorLabel;
	
	
	public LogInPanel(MyFrame frame){
		this.frame = frame;
		this.setLayout(null);
		this.setSize(frame.getSize());
		setUpLogInComponents();
	}

	private void setUpLogInComponents(){
		setUpEmail();
		setUpPassword();
		setUpLogInButton();
		setUpExitButton();
		setUpLogInErrorLabel();
	}
	private void setUpLogInErrorLabel(){
		logInErrorLabel = new JLabel("");
		logInErrorLabel.setBounds(100, 90, 200, 25);
		logInErrorLabel.setForeground(Color.red);
		this.add(logInErrorLabel);
		
	}
	private void setUpEmail(){
		JLabel emailLabel = new JLabel("Email:");
		emailLabel.setBounds(30, 37, 35, 10);
		this.add(emailLabel);

		emailField = new JTextField();
		emailField.setText("sgalka91@gmail.com");
		emailField.setBounds(100, 30, 250, 25);
		this.add(emailField);
	}
	private void setUpPassword(){
		JLabel passwordLabel = new JLabel("Password:");
		passwordLabel.setBounds(30, 77, 65, 10);
		this.add(passwordLabel);

		passwordField = new JPasswordField();
		passwordField.setText("password");
		passwordField.setBounds(100, 70, 250, 25);
		this.add(passwordField);
	}
	private void setUpLogInButton(){
		logIn = new JButton("Log in");
		logIn.setBounds(210, 130, 125, 25 );
		logIn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				String email = emailField.getText();
				String password = new String(passwordField.getPassword());
				User user = validateLogIn(email, password);
				if(user.getIPAddress() == null && user.getPort() == 0){
					logInErrorLabel.setText("Invalid Username or password");
				}
				else{
					Entities.network = new Network(user);
					Entities.network.connectAndroidDevice();
					frame.showControls();
					logInErrorLabel.setText("");
				}
		        	
			}
		});
		this.add(logIn);
	}
	private void setUpExitButton(){
		exit = new JButton("Exit");
		exit.setBounds(50, 130, 125, 25 );
		exit.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		this.add(exit);
	}
	public User validateLogIn(String email, String password){
		User user = new User(email, encript(password));
        try {
		    HttpClient httpclient = new DefaultHttpClient();
		    HttpPost httppost = new HttpPost("http://steveg.dyndns-web.com:8080/Capstone/ValidateLogin.do");

		    List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
		    nameValuePair.add(new BasicNameValuePair("user", new Gson().toJson(user)));
		    httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));
		    HttpResponse response = httpclient.execute(httppost);
		    user = new Gson().fromJson(EntityUtils.toString(response.getEntity()).trim(), User.class);	   
        } catch (Exception e) { }
        return user;
	}
	public String encript(String password){
		String encriptedPass = "";
		try{
		MessageDigest md = MessageDigest.getInstance("SHA");
		md.update(password.getBytes("UTF-8"));
		byte raw[] = md.digest();
		String hash = Base64.encode(raw);
		 encriptedPass = hash;
		}catch(Exception ex){}
		
		System.out.println(encriptedPass);
		return encriptedPass;
		
	}
}

